package com.fix.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.fix.dao.AuthDao;
import com.fix.entity.Authority;
import com.fix.entity.User;
import com.fix.util.DbPool;

public class AuthDaoImpl implements AuthDao {
	
	DbPool pool = DbPool.getPool();

	@Override
	public List<Authority> getAuthList() throws SQLException {
		String sql="SELECT * from authority ";
		Connection conn= pool.getConn();
		QueryRunner runner = new QueryRunner();
		List list = (List)runner.query(conn, sql, new BeanListHandler(Authority.class));
		//pool.back(conn);
		pool.back(conn);
		
		return list;
	}

	@Override
	public List<Authority> getAuthByUserid(int userid) throws SQLException {
		String sql="SELECT a.* " + 
				   "FROM `User` u " + 
				   "INNER JOIN User2Auth ua on u.uid = ua.userid " + 
				   "INNER JOIN Authority a on ua.authid = a.authid " + 
				   "WHERE uid = ?";
		
		Connection conn=  pool.getConn();
		QueryRunner runner = new QueryRunner();
		List list = (List)runner.query(conn, sql, new BeanListHandler(Authority.class),userid);
		pool.back(conn);
		return list;
	}

}
